<template>
  <div class="login-container" style="text-align: center;">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <h3 class="title">Work - 系统登录</h3>
      </div>
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="用户名"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="密码"
            name="password"
            tabindex="2"
            auto-complete="on"
            @keyup.native="checkCapslock"
            @blur="capsTooltip = false"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
      </el-tooltip>
      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登录</el-button>
      <div style="position:relative;text-align: left">
        <div class="tips"style="float: left">
          <span>保管好账号信息</span>
          <span>以免造成损失</span>
        </div>
        <br>
        <div class="thirdparty-button" >
          <a href="#">
            <el-image v-if="userlogin" :src="QRCode"/>
          </a>
          <a href="#"><svg-icon icon-class="smlogin" @mouseenter="enter()" @mouseleave="leave()"/></a>&nbsp;&nbsp;&nbsp;
<!--          <a :href="wechat"><svg-icon icon-class="wechat" /></a>&nbsp;&nbsp;&nbsp;-->
          <a :href="qq"><svg-icon icon-class="qqLogin" /></a>&nbsp;&nbsp;&nbsp;
          <a :href="gitee"><svg-icon icon-class="gitee" /></a>&nbsp;&nbsp;&nbsp;
        </div>
      </div>
    </el-form>
    <el-footer style="color: snow;font-family: 微软雅黑">
      <p><a href="http://www.beian.miit.gov.cn" target="_blank">豫ICP备18035176号-1</a><br></p>
      <p>© 2019 work100.com.cn 版权所有</p>
      <div style="margin-top: 30px;">
          <a href="https://bbs.work100.com.cn/" target="_blank" style="margin-left: 10px">work-论坛</a>
          <a href="https://bk.work100.com.cn/" target="_blank" style="margin-left: 10px">work-博客</a>　
          <a href="https://git.work100.com.cn/" target="_blank" style="margin-left: 10px">work-GitLab</a>
      </div>
      <p>微信公众号　　　微信小程序</p>
      <p>
        <el-image style="width: 100px;height: 100px;border-radius:10px;" src="https://bk.work100.com.cn/wp-content/themes/JieStyle-Two/images/wxgzh.jpg"/>
        <el-image style="width: 100px;height: 100px;margin-left: 30px;border-radius:10px;" src="https://bk.work100.com.cn/wp-content/themes/JieStyle-Two/images/wxxcx.jpg"/>
      </p>
    </el-footer>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { qrCode, qcStatus } from '@/api/user'
import { setToken, setExp, setTokenType, getTokenType } from '@/utils/auth'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value.trim()) {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 4) {
        callback(new Error('密码至少6位数'))
      } else {
        callback()
      }
    }
    return {
      QRCode: '',
      QrLoginCode: '',
      QRStatus: true,
      userlogin: false,
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined
    }
  },
  computed: {
    ...mapGetters([
      'druidApi',
      'qq',
      'gitee',
      'wechat'
    ])
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
  },
  destroyed() {
  },
  methods: {
    checkCapslock({ shiftKey, key } = {}) {
      if (key && key.length === 1) {
        if (shiftKey && (key >= 'a' && key <= 'z') || !shiftKey && (key >= 'A' && key <= 'Z')) {
          this.capsTooltip = true
        } else {
          this.capsTooltip = false
        }
      }
      if (key === 'CapsLock' && this.capsTooltip === true) {
        this.capsTooltip = false
      }
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || '/' })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    enter(){
      this.userlogin = true
      this.QRStatus = true;
      this.getScan()
    },
    leave(){
      this.userlogin = false
      this.QRCode = "";
      this.QRStatus = false;
    },
    getScan(){
      qrCode().then(res => {
        this.QRCode = res.data.data[0];
        if (res.data.code === 0){
          this.qrStatusfun(res.data.data[1])
        }
      })
    },
    qrStatusfun(uuid){
      const that = this
      const interval = setInterval(function(){
        qcStatus({ uuid: uuid}).then(res => {
          if (res.data.data === 0){
            that.$message.success(res.data.msg)
          } else if (res.data.data === 2){
            that.$message.success(res.data.msg)
            const { token, overdue, tokentype } = res.headers
            setToken(token)
            setExp(overdue)
            setTokenType(tokentype)
            window.location = '/'
            clearInterval(interval);
          }
        })
        if (!that.QRStatus)
          clearInterval(interval);
      }, 1500);

    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  position: absolute;
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
